/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

import com.core.utils.Utils;

/**
 *
 * @author daz
 */
public class ChessTable extends GameTable {

    private static final int NUMBER_CHESS_PLAYERS = 2;
    private static final int NUMBER_ROW = 8;
    private static final int NUMBER_COLUM = 8;
    private static final int EMPTY = -1;
    private static final int WHITE_KING = 5; // Vua
    private static final int WHITE_ROCK = 1; // Xe
    private static final int WHITE_BISHOP = 3; // Tượng
    private static final int WHITE_QUEEN = 4; // Hậu
    private static final int WHITE_KNIGHT = 2; // Mã
    private static final int WHITE_PAWN = 0; // Tốt
    private static final int BLACK_KING = 11;
    private static final int BLACK_ROCK = 7;
    private static final int BLACK_BISHOP = 9;
    private static final int BLACK_QUEEN = 10;
    private static final int BLACK_KNIGHT = 8;
    private static final int BLACK_PAWN = 6;
    private int[][] cells;
    private int winPlayerId;

    public ChessTable(GameManager gameManager, GameRoom parent, long moneyBetDefault) {
        super(GameManager.CHESS, NUMBER_CHESS_PLAYERS, gameManager, parent, moneyBetDefault);
        cells = new int[NUMBER_ROW][NUMBER_COLUM];
        this.tableMoneyBet = moneyBetDefault;
        initChessBoard();
    }

    private void initChessBoard() {
        for (int i = 0; i < NUMBER_ROW; i++) {
            for (int j = 0; j < NUMBER_COLUM; j++) {
                cells[i][j] = EMPTY;
            }
        }

        // Khởi tạo bàn cờ từ vị trí nhìn của người chơi cầm quân trắng
        cells[0][0] = BLACK_ROCK;
        cells[0][1] = BLACK_KNIGHT;
        cells[0][2] = BLACK_BISHOP;
        cells[0][3] = BLACK_QUEEN;
        cells[0][4] = BLACK_KING;
        cells[0][5] = BLACK_BISHOP;
        cells[0][6] = BLACK_KNIGHT;
        cells[0][7] = BLACK_ROCK;
        for (int i = 0; i < NUMBER_COLUM; i++) {
            cells[1][i] = BLACK_PAWN;
        }

        cells[7][0] = WHITE_ROCK;
        cells[7][1] = WHITE_KNIGHT;
        cells[7][2] = WHITE_BISHOP;
        cells[7][3] = WHITE_QUEEN;
        cells[7][4] = WHITE_KING;
        cells[7][5] = WHITE_BISHOP;
        cells[7][6] = WHITE_KNIGHT;
        cells[7][7] = WHITE_ROCK;
        for (int i = 0; i < NUMBER_COLUM; i++) {
            cells[6][i] = WHITE_PAWN;
        }
    }

    @Override
    public void processMessage(String request, int playerId) {
        super.processMessage(request, playerId);
        String[] message = Utils.split(request, '|');
        if (message[0].equals("UPG")) {
            int row, colum, chessId;
            row = Integer.parseInt(message[2]);
            colum = Integer.parseInt(message[3]);
            chessId = Integer.parseInt(message[1]);
            if (playerId == tableHost) {
                cells[row][colum] = chessId;
            } else {
                cells[NUMBER_ROW - 1 - row][NUMBER_COLUM - 1 - colum] = chessId;
            }
            sendToAll(request);
            return;
        }
        if (message[0].equals("EB")) {
            int oldRowKing, oldColumKing, oldRowRock, oldColumRock,
                    newRowKing, newColumKing, newRowRock, newColumRock;
            oldRowKing = Integer.parseInt(message[2]);
            oldColumKing = Integer.parseInt(message[3]);
            newRowKing = Integer.parseInt(message[4]);
            newColumKing = Integer.parseInt(message[5]);
            oldRowRock = Integer.parseInt(message[6]);
            oldColumRock = Integer.parseInt(message[7]);
            newRowRock = Integer.parseInt(message[8]);
            newColumRock = Integer.parseInt(message[9]);
            if (playerId == tableHost) {
                cells[oldRowKing][oldColumKing] = EMPTY;
                cells[oldRowRock][oldColumRock] = EMPTY;
                cells[newRowKing][newColumKing] = WHITE_KING;
                cells[newRowRock][newColumRock] = WHITE_ROCK;
            } else {
                cells[NUMBER_ROW - oldRowKing - 1][NUMBER_COLUM - oldColumKing - 1] = EMPTY;
                cells[NUMBER_ROW - oldRowRock - 1][NUMBER_COLUM - oldColumRock - 1] = EMPTY;
                cells[NUMBER_ROW - newRowKing - 1][NUMBER_COLUM - newColumKing - 1] = BLACK_KING;
                cells[NUMBER_ROW - newRowRock - 1][NUMBER_COLUM - newColumRock - 1] = BLACK_ROCK;
            }
            sendToAll(request);
            return;
        }
        if (message[0].equals("DRAW")) {
            sendToAll(request);
            return;
        }
        if (message[0].equals("AD")) {
            sendToAll(request);
            return;
        }
        if (message[0].equals("LOSE")) {
            sendToAll(request);
            winPlayerId = (playerId == 0) ? 1 : 0;
            computeMoney(winPlayerId);
            return;
        }
        if (message[0].equals("NEXT")) {
            sendToAll(request);
            return;
        }
        if (message[0].equals("CT")) {
            winPlayerId = Integer.parseInt(message[1]);
            theFirstTurn = winPlayerId;
            listPlayer[playerId].resetTimeDb();
            resetGame();
        }

        if (message[0].equals("STEP")) {
            int chessId, oldRow, oldColum, newRow, newColum;
            oldRow = Integer.parseInt(message[2]);
            oldColum = Integer.parseInt(message[3]);
            newRow = Integer.parseInt(message[4]);
            newColum = Integer.parseInt(message[5]);
            if (playerId == tableHost) {
                chessId = cells[oldRow][oldColum];
                cells[oldRow][oldColum] = EMPTY;
                cells[newRow][newColum] = chessId;
            } else {
                chessId = cells[NUMBER_ROW - 1 - oldRow][NUMBER_COLUM - 1 - oldColum];
                cells[NUMBER_ROW - 1 - oldRow][NUMBER_COLUM - 1 - oldColum] = EMPTY;
                cells[NUMBER_ROW - 1 - newRow][NUMBER_COLUM - 1 - newColum] = chessId;
            }
            if (playerId < maxPlayersInMatch) {
                listPlayer[playerId].setStartTimeDb(getNumberActivedPlayers());
            }
            sendToAll(request);
            return;
        }
        if (message[0].equals("CHECKMATE")) {
            sendToAll(request);
            return;
        }
        if (message[0].equals("FINISH")) {
            sendToAll(request);
            //reset ready state
            winPlayerId = Integer.parseInt(message[1]);
            computeMoney(winPlayerId);
            return;
        }



    }

    private void computeMoney(int winPlayerId) {
        StringBuilder msg1, msg2;
        long tempMoney, changeMoney;
        int lostPlayerId;
        msg1 = new StringBuilder();
        msg2 = new StringBuilder();
        msg1.append("CM");
        msg2.append("CM");
        lostPlayerId = (winPlayerId == 0) ? 1 : 0;
        tempMoney = listPlayer[lostPlayerId].getMoney();
        changeMoney = (tempMoney - tableMoneyBet >= 0) ? tableMoneyBet : tempMoney;
        tempMoney -= changeMoney;
        listPlayer[lostPlayerId].updateMoneyToDatabase(tempMoney);
        msg1.append("|").append(lostPlayerId).append("|-").append(changeMoney);
        sendToAll(msg1.toString());
        tempMoney = listPlayer[winPlayerId].getMoney();
        changeMoney = (int) (CHARGE * changeMoney);
        tempMoney += changeMoney;
        listPlayer[winPlayerId].updateMoneyToDatabase(tempMoney);
        msg2.append("|").append(winPlayerId).append("|").append(changeMoney);
        sendToAll(msg2.toString());
        sendToAll("FCM|");
    }

    protected void resetGame() {
        resetTable();
        initChessBoard();
    }

    @Override
    public void startPlay() {
        isPlay = true;
        for (int i = 0; i < numberUsers; i++) // start game
        {
            resetReadyState();
            listPlayer[i].response("START|" + theFirstTurn);
        }
    }

    @Override
    public void sendGameInfo() {
        if (isPlay) {
            StringBuilder msg = new StringBuilder();
            msg.append("CHESS");
            int temp;
            for (int i = 0; i < NUMBER_ROW; i++) {
                for (int j = 0; j < NUMBER_COLUM; j++) {
                    if (cells[i][j] != EMPTY) {
                        temp = i * NUMBER_COLUM + j;
                        msg.append("|").append(cells[i][j]).append("|").append(temp);
                    }
                }
            }
            if (viewIndex > maxPlayers && listPlayer[viewIndex - 1] != null) {
                listPlayer[viewIndex - 1].response(msg.toString());
            }
        }
    }
}
